<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>受控组件</title>
</head>
<body>
  <div id="root"></div>
  <script src="../js/react.development.js"></script>
  <script src="../js/react-dom.development.js"></script>
  <script src="../js/babel.min.js"></script>
  <script type="text/babel">
    class Demo extends React.Component{
      state = {
        username: '',
        password: ''
      }

      onChange = (dataType) => {
        return (e) => {
          this.setState({
            [dataType]: e.target.value
          })
        }
      }

      handleClick = () => {
        const { username, password } = this.state
        console.log(`提交了：${username}；${password}`)
      }

      render() {
        return (
          <div>
            <input type="text" value={this.state.username} onChange={this.onChange('username')} />
            <input type="text" value={this.state.password} onChange={this.onChange('password')} />
            <button onClick={this.handleClick}>点击提交</button>
            {/* 如果将，value 的值设置为 null 或者 undefined 则还是可输入；但如果是固定值，则会阻止用户输入
              <input type="text" value="hi" />
            */}
          </div>
        )
      }
    }
    ReactDOM.render(<Demo />, document.getElementById('root'))
  </script>
</body>
</html>